<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
model('core/core')->checklogin('jy_yiliao/index/');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
if($_RQ['op']=='main'){
	$activityid=intval($_RQ['activityid']);
	if(!$activityid){
		exi('请选择活动','error');
	}
	$activity=pdo_get('jy_yiliao_baoming_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$activityid));
	$activity['diyfrom']=$activity['diyfrom']?json_decode($activity['diyfrom'],true):array();
	foreach($activity['diyfrom'] as &$sv){
		if($sv['value']){
			$sv['value']=explode(';',$sv['value']);
			$new=array();
			foreach($sv['value'] as $ik=>&$iv){
				$new[$ik]['title']=$iv;
			}
			$sv['value']=$new;
		}
	}
	$post=array(
		'diyfields'=>$activity['diyfrom'],
		'activity_id'=>$activityid,
		'money'=>$activity['money']
	);
	$return['activity']=$activity;
	$return['post']=$post;
	exi($return);
}
//支付
if($_RQ['op']=='notice'){
	$post=get_postdata();
	//判断是否有数据
	if(count($post['diyfields'])==0){
		if(empty($post['name'])){
			exi('请填写姓名','error');
		}
		if(empty($post['mobile'])){
			exi('请填写手机号码','error');
		}
		if(!preg_match("/^1[345678]{1}\d{9}$/",$post['mobile'])){  	 
			exi('请输入正确的手机号码','error');
		}
	}else{
		foreach ($post['diyfields'] as $item) {
				if($item['ismust']==1 && empty($item['data'])){
					exi('请完善'.$item['title'].'内容后再提交','error');
				}
		}
	}
	$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'user_id'=>$_SESSION['uid'],
			'activity_id'=>$post['activity_id'],
			'money'=>$post['money'],
			'pay'=>$post['pay'],
			'name'=>$post['name'],
			'mobile'=>$post['mobile'],
			'remark'=>trim($post['remark']),
			'diyfrom'=>ijson_encode($post['diyfields']),
			'ordersn'=>get_ordersn('jy_yiliao_baoming_order','ordersn'),
			'createtime'=>TIMESTAMP,
	);
	pdo_insert('jy_yiliao_baoming_order',$insert);
	$id=pdo_insertid();
	if($post['money']>0){
		$this->session('money',$post['money']);
		$params['fee']=$post['money'];
		$params['title']='活动报名订单';
		$params['id']=$id;
		$params['plugin']='jy_yiliao';
		$params['back_url']='jy_yiliao/baoming.book/payresult/';
		$payid=cfc('pay')->create_pay_order($params);
		$this->pdo_update('order',array('pay_id'=>$payid),array('id'=>$id));
		$res=array(
				'pay_id'=>$payid,
				'order_id'=>$id,
		);
		exi($res);
	}else{
		pdo_update('jy_yiliao_baoming_order',array('status'=>'2'),array('id'=>$id));
		exi('报名成功','success','jy_yiliao/baoming.order/list/status=2&id='.$id);
	}
	
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result']; 
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','main');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','main');
	}
	$order=pdo_get('jy_yiliao_baoming_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	$order['price']=$this->session('money');
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['status']){
		exi('订单已支付，无需重复支付！','error');
	}
	if($order['money']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_yiliao_baoming_order',array('status'=>'2','pay'=>$payresult['fee']),array('id'=>$order['id']));
	$this->model('commission')->upfee($order,'活动报名订单');
	exi('支付成功','success','jy_yiliao/baoming.order/list/status=2&id='.$order['id']);
}